﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using TheOliver.DigitalSignage.Admin.Controls;
using Web;
using Web.Services;

namespace TheOliver.DigitalSignage.Admin.Views
{
    public partial class Rooms : Page
    {
        public Rooms()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void eventDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void roomDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void _addRoom_Click(object sender, RoutedEventArgs e)
        {
            Web.Event ev = _eventCombo.SelectedItem as Web.Event;

            Room room = new Room();
            room.EventId = ev.Id;
            room.Name = "New Room for " + ev.Name;
            room.Description = "New Room";

            DigitalSignageDomainContext context =
                roomDomainDataSource.DomainContext as DigitalSignageDomainContext;
            
            context.Rooms.Add(room);
        }

        private void _saveRooms_Click(object sender, RoutedEventArgs e)
        {
            roomDomainDataSource.SubmitChanges();
        }

        private void _rejectRooms_Click(object sender, RoutedEventArgs e)
        {
            roomDomainDataSource.RejectChanges();
        }

        private void _removeRoom_Click(object sender, RoutedEventArgs e)
        {
            if (roomDataGrid.SelectedItem != null)
            {
                QuestionWindow qw = 
                    new QuestionWindow(TextResource.DeleteRoomQuestion, DeleteRoom);
                qw.Show();
            }
        }

        private void DeleteRoom()
        {
            Web.Room room = roomDataGrid.SelectedItem as Web.Room;

            DigitalSignageDomainContext context =
                roomDomainDataSource.DomainContext as DigitalSignageDomainContext;
            
            context.Rooms.Remove(room);
        }
    }
}
